//
//  PCUserModel.m
//  PhotoCircle
//
//  Created by GL on 15/12/15.
//  Copyright © 2015年 wujungao. All rights reserved.
//

#import "PCUserModel.h"

#define KEY_USER_ID             @"uuid"
#define KEY_USER_Token          @"token"

#define KEY_USER_Name           @"name"
#define KEY_USER_Gender         @"gender"
#define KEY_USER_Logo           @"logo"
#define KEY_USER_Image          @"image"

#define KEY_UESR_AlbumCount     @"album_photos_count"
#define KEY_UESR_GameCount      @"game_photos_count"
#define KEY_UESR_Score          @"score" // 用户积分

@interface PCUserModel () <NSCoding>

@end

@implementation PCUserModel

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self)
    {
        _userID = [aDecoder decodeObjectForKey:KEY_USER_ID];
        _userToken = [aDecoder decodeObjectForKey:KEY_USER_Token];
        _userName  = [aDecoder decodeObjectForKey:KEY_USER_Name];
        _userGender = [aDecoder decodeObjectForKey:KEY_USER_Gender];
        _userLogoUrl = [aDecoder decodeObjectForKey:KEY_USER_Logo];
        _userImageData = [aDecoder decodeObjectForKey:KEY_USER_Image];
        
        _userAlbumCount    = [aDecoder decodeObjectForKey:KEY_UESR_AlbumCount];
        _userGameCount     = [aDecoder decodeObjectForKey:KEY_UESR_GameCount];
        _userScore         = [aDecoder decodeObjectForKey:KEY_UESR_Score];

    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:_userID forKey:KEY_USER_ID];
    [aCoder encodeObject:_userToken forKey:KEY_USER_Token];
    [aCoder encodeObject:_userName forKey:KEY_USER_Name];
    [aCoder encodeObject:_userGender forKey:KEY_USER_Gender];
    [aCoder encodeObject:_userLogoUrl forKey:KEY_USER_Logo];
    [aCoder encodeObject:_userImageData forKey:KEY_USER_Image];
    
    [aCoder encodeObject:_userAlbumCount forKey:KEY_UESR_AlbumCount];
    [aCoder encodeObject:_userGameCount  forKey:KEY_UESR_GameCount];
    [aCoder encodeObject:_userScore  forKey:KEY_UESR_Score];

}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super initWithDictionary:dict];
    if (self) {
        
        NSLog(@"dict :%@",dict);
        _userID = dict[KEY_USER_ID];
        _userToken = dict[KEY_USER_Token];
        
        if (dict[KEY_USER_Name]!=[NSNull null]) {
            _userName = dict[KEY_USER_Name];
        }
        if (dict[KEY_USER_Gender]!=[NSNull null]) {
            _userGender = dict[KEY_USER_Gender];
        }
        if (dict[KEY_USER_Logo]!=[NSNull null]) {
            _userLogoUrl = dict[KEY_USER_Logo];
            _userImageData = [NSData dataWithContentsOfURL:PCURL(_userLogoUrl)];
        }
        if (dict[KEY_UESR_AlbumCount]!=[NSNull null]) {
            
            NSNumber *albumNumber = (NSNumber*)dict[KEY_UESR_AlbumCount];
            NSString *aCount = [albumNumber stringValue];
            _userAlbumCount = aCount;
        }
        if (dict[KEY_UESR_GameCount]!=[NSNull null]) {
            NSNumber *albumNumber = (NSNumber*)dict[KEY_UESR_GameCount];
            NSString *aCount = [albumNumber stringValue];
            _userGameCount = aCount;
        }
        
        if (dict[KEY_UESR_Score]!=[NSNull null]) {
            NSNumber *scoreNumber = (NSNumber*)dict[KEY_UESR_Score];
            NSString *aScore = [scoreNumber stringValue];
            _userScore = aScore;
        }
        
    }
    return self;
}

@end
